package de.uni.webprog.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;

public class Calc implements EntryPoint {

	Integer erg = 0;
	Double erg1 = 0.0;
	Integer operand = 0;
	Boolean isEmpty = true;
	
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
				
				FlexTable flexTable = new FlexTable();
				rootPanel.add(flexTable, 49, 45);
				flexTable.setSize("98px", "158px");
						
				final IntegerBox integerBox = new IntegerBox();
				integerBox.setAlignment(TextAlignment.RIGHT);
				integerBox.setText("0");
				flexTable.setWidget(0, 0, integerBox);
				integerBox.setSize("96%", "16px");
						
				final Button btn7 = new Button("7");
				flexTable.setWidget(1, 0, btn7);
				btn7.setText("7");
				btn7.setSize("28px", "28px");
				btn7.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn7.getText(), integerBox);
					}
				});
					
				final Button btn8 = new Button("8");
				flexTable.setWidget(1, 1, btn8);
				btn8.setText("8");
				btn8.setSize("28px", "28px");
				btn8.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn8.getText(), integerBox);
					}
				});
					
				final Button btn9 = new Button("9");
				flexTable.setWidget(1, 2, btn9);
				btn9.setText("9");
				btn9.setSize("28px", "28px");
				btn9.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn9.getText(), integerBox);
					}	
				});
				
				final Button btn4 = new Button("4");
				flexTable.setWidget(2, 0, btn4);
				btn4.setText("4");
				btn4.setSize("28px", "28px");
				btn4.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn4.getText(), integerBox);
					}
				});
						
				final Button btn5 = new Button("5");
				flexTable.setWidget(2, 1, btn5);
				btn5.setText("5");
				btn5.setEnabled(true);
				btn5.setSize("28px", "28px");
				btn5.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn5.getText(), integerBox);
					}
				});
				
				final Button btn6 = new Button("6");
				flexTable.setWidget(2, 2, btn6);
				btn6.setText("6");
				btn6.setEnabled(true);
				btn6.setSize("28px", "28px");
				btn6.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn6.getText(), integerBox);
					}
				});
						
				final Button btn1 = new Button("1");
				flexTable.setWidget(3, 0, btn1);
				btn1.setText("1");
				btn1.setEnabled(true);
				btn1.setSize("28px", "28px");
				btn1.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn1.getText(), integerBox);
					}
				});
					
				final Button btn2 = new Button("2");
				flexTable.setWidget(3, 1, btn2);
				btn2.setText("2");
				btn2.setEnabled(true);
				btn2.setSize("28px", "28px");
				btn2.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn2.getText(), integerBox);
					}
				});
					
				final Button btn3 = new Button("3");
				flexTable.setWidget(3, 2, btn3);
				btn3.setText("3");
				btn3.setSize("28px", "28px");
				btn3.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn3.getText(), integerBox);
					}
				});
				
				final Button btn_clear = new Button("ce");
				flexTable.setWidget(4, 0, btn_clear);
				btn_clear.setSize("28px", "28px");
				btn_clear.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn_clear.getText(), integerBox);
					}
				});
				
				final Button btn0 = new Button("0");
				flexTable.setWidget(4, 1, btn0);
				btn0.setText("0");
				btn0.setSize("28px", "28px");
				flexTable.getFlexCellFormatter().setColSpan(0, 0, 4);
				btn0.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn0.getText(), integerBox);
					}
				});
				
				final Button btn_div = new Button("/");
				btn_div.setSize("28px", "28px");
				btn_div.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn_div.getText(), integerBox);
					}
				});
								
				final Button btn_erg = new Button("=");
				flexTable.setWidget(4, 2, btn_erg);
				btn_erg.setSize("28px", "28px");
				flexTable.setWidget(4, 3, btn_div);
				btn_erg.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn_erg.getText(), integerBox);
					}
				});

				final Button btn_plus = new Button("+");
				flexTable.setWidget(1, 3, btn_plus);
				btn_plus.setText("+");
				btn_plus.setEnabled(true);
				btn_plus.setSize("28px", "28px");
				btn_plus.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn_plus.getText(), integerBox);
					}
				});
				
				final Button btn_minus = new Button("-");
				flexTable.setWidget(2, 3, btn_minus);
				btn_minus.setText("-");
				btn_minus.setSize("28px", "28px");
				btn_minus.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn_minus.getText(), integerBox);
					}
				});
				
				final Button btn_multi= new Button("*");
				flexTable.setWidget(3, 3, btn_multi);
				btn_multi.setSize("28px", "28px");
				
				Label lblNewLabel = new Label("Calculator");
				rootPanel.add(lblNewLabel, 86, 10);
				lblNewLabel.setSize("64px", "26px");
				btn_multi.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						changeLabel(btn_multi.getText(), integerBox);
					}
				});
		}	
	
	private void changeLabel(String number, IntegerBox intBox) {
		if (number.equals("+")) {
			operand = 1;
			erg = Integer.parseInt(intBox.getText());
			isEmpty = true;
		} else {if (number.equals("-")) {
			operand = 2;
			erg = Integer.parseInt(intBox.getText());
			isEmpty = true;
		} else if (number.equals("*")) {
			operand = 3;
			erg = Integer.parseInt(intBox.getText());
			isEmpty = true;
		} else if (number.equals("/")) {
			operand = 4;
			erg = Integer.parseInt(intBox.getText());
			isEmpty = true;
		} else if (number.equals("ce")) {
			intBox.setText("0");
			isEmpty = true;
		} else if (number.equals("=")) {
			switch (operand) {
			  case 1: 
			    erg1 = erg + Double.parseDouble(intBox.getText());
			    break;
			  case 2: 
				  erg1 = erg - Double.parseDouble(intBox.getText());
			    break;
			  case 3: 
				  erg1 = erg * Double.parseDouble(intBox.getText());
			    break;
			  case 4: 
				  erg1 = erg / Double.parseDouble(intBox.getText());
			    break;
			  } 
			  intBox.setText(erg1.toString());
			erg = 0;
			erg1 = 0.0;
			operand = 0;
			isEmpty = true;}
		else if (isEmpty == true) {
			intBox.setText(number);
			isEmpty = false;
		} else {
			intBox.setText(intBox.getText() + number);
		}}
		//erg = Integer.parseInt(intBox.getText());
		
	}
}
